<template>
  <div class="app-container" style="overflow:auto;">
    <el-header>
      <div class="table-filter" >
      <el-input
        class="seacher-input"
        placeholder="请输入ID查询"
        prefix-icon="el-icon-search"
        v-model="seacherStr"
      >
      </el-input>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handGetItems(10,1)">
        搜索
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-document-add" @click="handleCreate">
        创建
      </el-button>
      </div>
    </el-header>
    <el-table
      :data="tableDatas"
      border
      style="width: 100%"
      highlight-current-row
      v-loading="listLoading"
      element-loading-text="loading"
      max-height="400"
    >
        <el-table-column prop="_id" label="ID" align="center" width="200"> </el-table-column>
        <el-table-column prop="title" label="标题" show-overflow-tooltip align="center" ></el-table-column>
        <el-table-column label="操作" width="200" align="center">
          <template #default="scope">
            <el-button @click="handleEeit(scope.row)" type="danger" size="small">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    <el-footer>
      <el-pagination
        :page-size=paginationDate.rows
        :total=paginationDate.total
        :current-page=paginationDate.cuurentPage
        :page-sizes=paginationDate.pageSizes
        layout='total, prev, pager, next, jumper, sizes, slot'
        @size-change='handleSizeChange'
        @current-change='handleCurrentChange'
        style="line-height: 30px; min-width:300px;"
      >
        <i @click="handReload" class="el-icon-refresh"></i>
      </el-pagination>
    </el-footer>

  </div>
</template>
<script>
import { getItems } from '@/cloudbaseapi/cloudbase-hd'
export default {
  data() {
    return {
      tableDatas: [],
      paginationDate: {
        total: 10,
        cuurentPage: 1,
        rows: 10,
        pageSizes: [10, 20, 30, 50]
      },
      listLoading: true,
      shiJiType: [],
      seacherStr: '',
      seacherShjJiType: -1
    }
  },
  created() {
    this.handGetItems()
  },
  mounted() {
    document.onkeypress = e => {
      console.log(e.keyCode)
      if (e.keyCode === 13) {
        this.handGetItems(10, 1)
      }
    }
  },
  methods: {
    handGetItems(item = 10, page = 1) {
      this.listLoading = true
      let _id = null
      if (this.seacherStr !== '') {
        _id = this.seacherStr
      }
      getItems(item, page, _id).then(items => {
        this.paginationDate.total = items.total
        this.tableDatas = items.data
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      })
    },
    handleCreate(row) {
      this.$router.push('hdcreate')
    },
    handleEeit(row) {
      this.$router.push({
        name: 'hdEdit',
        query: { id: row._id }
      })
    },
    handleSizeChange(val) {
      this.handGetItems(val, this.paginationDate.cuurentPage)
      this.paginationDate.rows = val
    },
    handleCurrentChange(val) {
      this.handGetItems(this.paginationDate.rows, val)
      this.paginationDate.cuurentPage = val
    },
    handReload() {
      this.handGetItems(this.paginationDate.rows, this.paginationDate.cuurentPage)
    }
  }
}
</script>

<style scoped lang="scss">
  .table-filter{
    display: flex;
    flex-direction: row;
    .seacher-input{
      width:200px;
      margin-right:20px;
    }

  }
</style>
